﻿using UnityEngine;
using System.Collections;

public class MovablePlatform : MonoBehaviour {

    public Vector2 offsets;
    public float speed;
    public Orientation orientation;

    // Min = Offsets.x
    // Max = Offsets.y

    void Start()
    {
        switch (orientation)
        {
            case Orientation.HORIZONTAL:
                if (Mathf.Abs(transform.position.x - offsets.x) > Mathf.Abs(transform.position.x - offsets.y))
                    speed = -speed;
                break;

            case Orientation.VERTICAL:
                if (Mathf.Abs(transform.position.y - offsets.x) > Mathf.Abs(transform.position.y - offsets.y))
                    speed = -speed;
                break;
        }
    }
	void Update () 
    {
        switch (orientation)
        { 
            case Orientation.HORIZONTAL:
                transform.Translate(speed * Time.deltaTime, 0, 0);

                if (transform.position.x < offsets.x)
                {
                    transform.position = new Vector3(offsets.x, transform.position.y, transform.position.z);
                    speed = -speed;
                }
                if (transform.position.x > offsets.y)
                {
                    transform.position = new Vector3(offsets.y, transform.position.y, transform.position.z);
                    speed = -speed;
                }
                break;

            case Orientation.VERTICAL:
                transform.Translate(0,speed * Time.deltaTime, 0);

                if (transform.position.y < offsets.x)
                {
                    transform.position = new Vector3(transform.position.x, offsets.x, transform.position.z);
                    speed = -speed;
                }
                if (transform.position.y > offsets.y)
                {
                    transform.position = new Vector3(transform.position.x, offsets.y, transform.position.z);
                    speed = -speed;
                }
                break;
        }
	}

    public enum Orientation
    { 
        VERTICAL,
        HORIZONTAL
    }
}
